using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MyFrame.Mono;
using MyFrame.Pool;
using Platform.CharacterData;
using Platform.Skill.Deployer;
using UnityEngine;

namespace GardeningSkeleton.Platform.SkillSpace
{

    /// <summary>
    /// 回弹效果   考虑把旋转动画向上提
    /// </summary>
    public class ReBoundImpact : BaseImpactEffect
    {

        ParabolicSkillDeployer deployer;

        //该技能特有属性
        public bool isBouncing = false;
        public float BoundingSpeed = 25f;

        private int currentTargetIndex = 0;
        private Transform[] targetsArray;


        public override void OnImpactTriggerEntry(Collider2D other)
        {
            deployer.StopRigidbody();
            // 范围检查
            deployer.chooseTargets();

            // 如果弹跳继续旋转
            if (other.gameObject.layer == LayerMask.NameToLayer("Enemy") && skillData.attackTargets.Count > 0)
            {
                isBouncing = true;
                // 将 HashSet 转换为数组，这样可以使用索引
                targetsArray = skillData.attackTargets.ToArray();
                return;
            }
            deployer.anim.SetBool("Rotation", false);
            deployer.transform.parent = other.transform;
        }

        public override void Execute(SkillDeployer baseDeployer)
        {
            this.deployer = (ThrowSwordSkillDeployer)baseDeployer;

            this.deployer.anim.SetBool("Rotation", true);

            MonoMgr.Instance.AddUpdateEvent(ReturnSwordUpdate);
        }

        public void ReturnSwordUpdate()
        {
            if (isBouncing && targetsArray?.Length > 0)
            {
                if (currentTargetIndex >= targetsArray.Length)
                {
                    // 如果已经遍历完所有目标，重置索引或结束反弹
                    currentTargetIndex = 0;
                    if (skillData.combo >= skillData.comboCount)
                    {
                        isBouncing = false;
                        deployer.ReturnSword();
                        return;
                    }
                }

                var currentTarget = targetsArray[currentTargetIndex];
                var entity = currentTarget.GetComponent<Enemy>();

                // 移动到当前目标
                deployer.transform.position = Vector2.MoveTowards(deployer.transform.position,
                    entity.GetRealPos(), BoundingSpeed * Time.deltaTime);

                // 检查是否已到达当前目标
                if (Vector2.Distance(deployer.transform.position, entity.GetRealPos()) < 0.1f)
                {
                    deployer.AttackStart(entity.transform);
                    skillData.combo++;

                    // 移动到下一个目标
                    currentTargetIndex++;

                    // 如果已经完成所有反弹，结束反弹
                    if (skillData.combo >= skillData.comboCount)
                    {
                        skillData.combo = 0;
                        isBouncing = false;
                        deployer.ReturnSword();
                    }
                }
            }
        }

        public override void EndEffect()
        {
            deployer.ReturnSword();
            MonoMgr.Instance.RemoveUpdateEvent(ReturnSwordUpdate);
        }


    }
}